//
//  Demo5.m
//  PezMonkeyTest
//
//  Created by Spence DiNicolantonio on 11/3/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import "Demo5.h"


@implementation Demo5

- (void)initDemo {
	// create space
	PMSpace *newSpace = [[PMSpace alloc] init];
	[newSpace setStepsPerFrame:3];
	[newSpace setIterations:20];
	[newSpace setGravity:PMVectMake(0, -1)];
	self.space = newSpace;
	[newSpace release];
	
	// resize spacial hash to improve performance
	[space setActiveHashSize:1999 cellDimension:40];
	[space setStaticHashSize:99 cellDimension:40];
	
	// define domnio vertices
	PMVect dominoVerts[] = {
		PMVectMake(-3,-20),
		PMVectMake(-3, 20),
		PMVectMake( 3, 20),
		PMVectMake( 3,-20),
	};
	
	// add a floor
	PMShape *shape = [[PMSegmentShape alloc] initWithBody:[PMBody staticBody] 
										   start:PMVectMake(-600, -240) 
											 end:PMVectMake(600, -240)];
	[shape setElasticity:1.0];
	[shape setFriction:1.0];
	[space addStaticShape:shape];
	[shape release];
	
	// shared constants
	float friction = 0.6;
	float elasticity = 0.0;
	float mass = 1.0;
	float moment = PMMomentForPolygon(mass, dominoVerts, PMVectZero);
	
	PMBody *body;
	PMVect position;
	NSUInteger baseCount = 9;
	for(int i=1; i<=baseCount; i++){
		PMVect offset = PMVectMake(-i*60/2.0f, (baseCount - i)*52);
		
		for(int j=0; j<i; j++){
			// define position of body
			position = PMVectAdd(PMVectMake(j*60, -220), offset);
			
			// create body and add to space
			body = [[PMBody alloc] initWithMass:mass Moment:moment];
			[body setPosition:position];
			[space addBody:body];
			
			// create polygon collision shape and add to space
			shape = [[PMPolygonShape alloc] initWithBody:body 
											 vertexArray:dominoVerts];
			[shape setFriction:friction];
			[shape setElasticity:elasticity];
			[space addShape:shape];
			
			// release the body and shape
			[body release];
			[shape release];
			
			
			
			// define position of body
			position = PMVectAdd(PMVectMake(j*60, -197), offset);
			
			// create body and add to space
			body = [[PMBody alloc] initWithMass:mass Moment:moment];
			[body setPosition:position];
			[body setAngle:M_PI/2.0];
			[space addBody:body];
			
			// create polygon collision shape and add to space
			shape = [[PMPolygonShape alloc] initWithBody:body 
											 vertexArray:dominoVerts];
			[shape setFriction:friction];
			[shape setElasticity:elasticity];
			[space addShape:shape];
			
			// release the body and shape
			[body release];
			[shape release];
			
			
			
			if(j == (i - 1)) continue;
			
			// define position of body
			position = PMVectAdd(PMVectMake(j*60 + 30, -191), offset);
			
			// create body and add to space
			body = [[PMBody alloc] initWithMass:mass Moment:moment];
			[body setPosition:position];
			[body setAngle:M_PI/2.0];
			[space addBody:body];
			
			// create polygon collision shape and add to space
			shape = [[PMPolygonShape alloc] initWithBody:body 
											 vertexArray:dominoVerts];
			[shape setFriction:friction];
			[shape setElasticity:elasticity];
			[space addShape:shape];
			
			// release the body and shape
			[body release];
			[shape release];
		}
		
		// define position of body
		position = PMVectAdd(PMVectMake(-17, -174), offset);
		
		// create body and add to space
		body = [[PMBody alloc] initWithMass:mass Moment:moment];
		[body setPosition:position];
		[space addBody:body];
		
		// create polygon collision shape and add to space
		shape = [[PMPolygonShape alloc] initWithBody:body 
										 vertexArray:dominoVerts];
		[shape setFriction:friction];
		[shape setElasticity:elasticity];
		[space addShape:shape];
		
		// release the body and shape
		[body release];
		[shape release];	
		
		
		
		// define position of body
		position = PMVectAdd(PMVectMake((i - 1)*60 + 17, -174), offset);
		
		// create body and add to space
		body = [[PMBody alloc] initWithMass:mass Moment:moment];
		[body setPosition:position];
		[space addBody:body];
		
		// create polygon collision shape and add to space
		shape = [[PMPolygonShape alloc] initWithBody:body 
										 vertexArray:dominoVerts];
		[shape setFriction:friction];
		[shape setElasticity:elasticity];
		[space addShape:shape];
		
		// release the body and shape
		[body release];
		[shape release];	
	}
}

@end
